package com.alks.function.controller.finance.mtlcosts;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.common.utils.beanutils.ArrayUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.finance.mtlcosts.FinSuppDutyTtl2Dto;
import com.alks.function.data.dto.finance.ryfinance.CostDetailDto;
import com.alks.function.data.request.finance.mtlcosts.SuppDutyTtlFindRequest;
import com.alks.function.data.request.finance.mtlcosts.SuppDutyTtlQueryRequest;
import com.alks.function.service.finance.mtlcosts.ISuppDutyFinaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author MS
 * @since 2024/6/22下午5:03
 */
@RestController
@ApiRestController("/mtlCosts")
@Api(value = "财务部", tags = "财务部-材料费用-补单扣款作业-3")
@Slf4j
public class SuppDutyFinaController {
    @Autowired
    private ISuppDutyFinaService service;

    @WebLogger
    @PostMapping("/suppDutyTtlQuery")
    @ApiOperation(value = "入库明细查询", tags = "财务部-材料费用-补单扣款作业-3")
    public ResponseInfo suppDutyTtlQuery(@RequestBody SuppDutyTtlQueryRequest request) {
        return ResponseInfo.ok(service.suppDutyTtlQuery(request));
    }

    @WebLogger
    @PutMapping("/suppDutyTtlFinance")
    @ApiOperation(value = "对账作业", tags = "财务部-材料费用-补单扣款作业-3")
    public ResponseInfo suppDutyTtlFinance(@RequestBody List<SuppDutyTtlFindRequest> request,
                                           @RequestParam(required = false)@ApiParam(value = "对账月份") String financeYymm,
                                           @RequestParam(required = false)@ApiParam(value = "对账方式") String accountName,
                                           @RequestParam@ApiParam(required = true, value = "0.取消对账 1.对账 3.仅修改备注") Integer flag,
                                           @RequestParam(required = false)@ApiParam("备注") String remark) {
        return service.suppDutyTtlFinance(request, financeYymm, accountName, flag ,remark);
    }

    @WebLogger
    @GetMapping("/suppDutyTtlFinBack")
    @ApiOperation(value = "备注信息回显查询", tags = "财务部-材料费用-补单扣款作业-3")
    public ResponseInfo mtlPreOtherInBack() {
        return service.mtlPreOtherInBack();
    }

    @WebLogger
    @PostMapping("/suppDuty/export")
    @ApiOperation(value = "导出", tags = "财务部-材料费用-补单扣款作业-3")
    public void suppDutyExport(@RequestBody SuppDutyTtlQueryRequest request, HttpServletResponse response) {
        request.setPageSize(-1);
        String templet = "补单扣款作业_3模版.xls";
        String fileName = "补料责任归属_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMdd")) + ".xls";
        Map<String, Object> map = service.suppDutyTtlQuery(request);
        List<FinSuppDutyTtl2Dto> dtoList = (List<FinSuppDutyTtl2Dto>) map.get("dtos");
        if (ArrayUtils.isEmpyt(dtoList)){
            throw new ServiceErrorException("暂无数据可导出");
        }
        FinSuppDutyTtl2Dto dto = new FinSuppDutyTtl2Dto();
        /*补充合计*/
        BigDecimal amtSum = dtoList.stream()
                .map(FinSuppDutyTtl2Dto::getAmt)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        dto.setAmt(amtSum);
        dtoList.add(dto);
        /*生成EXCEL*/
        try {
            ExcelUtils.creatByTemplate(response, templet, fileName, dtoList);
        } catch (Exception e) {
            throw new ServiceErrorException("导出失败");
        }
    }

}